#!/bin/bash

. /etc/init.d/functions

# 判断执行用户
if [ `id -u` != 0 ]; then
    echo -e "\033[43;30mPermission denied ! Please use root user\033[0m"
    exit 1
fi

#检测本机是否有scp命令
type scp
if [ $? != 0 ]; then
    echo -e "\033[43;30m本机没有scp命令,请检测!\033[0m"
    exit 1
fi

currPath=$(cd "$(dirname "$0")"; pwd)
export localPath=$currPath

printf "%-10s %-8s\n" 编号 安装方式
printf "%-10s %-8s\n" 1 单机
printf "%-10s %-8s\n" 2 集群
printf "%-10s %-8s\n" 3 哨兵

echo -e "请输入安装方式[\e[1;31m编号数字\e[0m]:"
read -p "Please input: " NUM
if [ "${NUM}" == "1" ] || [ "${NUM}" == "2" ] || [ "${NUM}" == "3" ];then
    # install 
    if [ "${NUM}" -eq "1" ]; then
        echo -e "开始安装[\e[1;31m单机\e[0m]模式"
	    redis_i=`source $localPath/bin/readconf.sh single ip`
        local local_ip=$(hostname -I | awk '{print $1}')
        file_ip=$(echo $redis_i | awk -F '"' '{print $4}')
        if [ "$local_ip" == "$file_ip" ]; then
            sh $localPath/bin/single.sh $redis_i
        else
            sh $localPath/bin/single_remote.sh $redis_i
        fi
    elif [ "${NUM}" -eq "2" ];then
        echo -e "开始安装[\e[1;31m三主三从集群\e[0m]模式"
        redis_i=`source $localPath/bin/readconf.sh cluster ipport`
        sh $localPath/bin/cluster.sh $redis_i
    else
        echo -e "开始安装[\e[1;31m哨兵\e[0m]模式"
    fi
else
    echo '无效的输入'
    exit 1
fi
